package cn.goktech.sports.modules.importDataone.service.impl;

import cn.goktech.sports.common.entity.R;
import cn.goktech.sports.modules.importDataone.dao.UserMapper;
import cn.goktech.sports.modules.importDataone.entity.*;
import cn.goktech.sports.modules.importDataone.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br>
 * 〈用户数据实现〉
 *
 * @author PanY
 * @create 2019/10/15
 * @since 1.0.0
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    UserMapper userMapper;


    @Override
    public UserEntity selectUserByName(String name) {
        return userMapper.selectUserByName(name);
    }

    @Override
    public boolean insertUserEntity(UserEntity user) {
        return userMapper.insertUserEntity(user);
    }

    @Override
    public boolean updateUserEntity(UserEntity user) {
        return userMapper.updatetUserEntity(user);
    }

    @Override
    public UserEntity selectUserByNameAndPwd(String name, String password) {
        return userMapper.selectUserByNameAndPwd(name, password);
    }


    @Override
    public List<OriginalPlaceEntity> getAreaList() {
        List<OriginalPlaceEntity> list = userMapper.getAreaList();
        List<OriginalPlaceEntity> resultList = buildByRecursive(list);
        return resultList;

    }


    public List<BaseCountry> getLevelCountryList() {
        List<BaseCountry> countryList = userMapper.getCountryList();
        List<BaseCountry> resultList = buildCountryList(countryList);
        return resultList;
    }
    public List<BaseCountry> getCountryList() {
        List<BaseCountry> countryList = userMapper.getCountryList();
        return countryList;
    }


    public List<Depot> getDepotList(Long countryId) {
        List<Depot> depotList = userMapper.getDepotList(countryId);

        return depotList;
    }

    @Override
    public List<NationalWeightEntity> getNationAreaList(String national_area_name) {
        return userMapper.getNationAreaList(national_area_name);
    }

    @Override
    public List<HeadProcessEntity> getHeadProcess(String name) {
        return userMapper.getHeadProcess(name);
    }

    @Override
    public List<ChargingSectionEntity> getChargingSectionList(Long header_id) {
        return userMapper.getChargingSectionList(header_id);
    }

    @Override
    public Map<String, Object> getFuel(Long fuel_id) {
        return userMapper.getFuel(fuel_id);
    }

    @Override
    public List<Map<String, Object>> getAttachCost(Long header_id) {
        return userMapper.getAttachCost(header_id);
    }

    @Override
    public List<HeadWayEntity> getHeaderLandWay(String name, int weight) {
        return userMapper.getHeaderLandWay(name, weight);
    }


    @Override
    public List<HeaderSeaEntity> getHeaderSea(String name) {
        return userMapper.getHeaderSea(name);
    }

    @Override
    public List<ShippingCharging> getHeaderSeaCharging(Long sea_id) {
        return userMapper.getHeaderSeaCharging(sea_id);
    }

    @Override
    public List<HeaderRailwayEntity> getHeaderRailway(String name) {
        return userMapper.getHeaderRailway(name);
    }

    @Override
    public List<HeaderRailwaySectionEntity> getHeaderRailwaySection(Long railway_id) {
        return userMapper.getHeaderRailwaySection(railway_id);
    }

    @Override
    public R getGoodsNameNum(String goodsName, Long depotId, Long countryId) {
        List<Map<String, String>> prohibitedNum = userMapper.getProhibitedNum(goodsName, countryId);
        String msg ="";
        for (int i=0;i<prohibitedNum.size();i++){
            if ( StringUtils.isNotBlank(prohibitedNum.get(i).get("remark")) ){
                msg = "当前的物品："+goodsName+",满足"+prohibitedNum.get(i).get("name")+"，属于违禁品，解决方案："+prohibitedNum.get(i).get("remark");
                break;
            }else{
                msg = "当前的物品："+goodsName+",满足"+prohibitedNum.get(i).get("name")+"，属于违禁品，请知晓！";
            }
        }
        if (StringUtils.isBlank(msg)){
            return R.ok("验证通过");
        }
        return R.error(msg);
    }

    @Override
    public List<Map<String, Object>> getThroughSection(Long country_partition_id, Long product_type_id) {
        return userMapper.getThroughSection(country_partition_id,product_type_id);
    }

    @Override
    public List<Map<String, Object>> getDhlCharging(Long country_partition_id, Long product_type_id) {
        return userMapper.getDhlCharging(country_partition_id,product_type_id);
    }

    @Override
    public Map<String, Object> getFedExCharging(Long partition_id, Long product_type_id, BigDecimal weight) {
        return userMapper.getFedExCharging(partition_id,product_type_id,weight);
    }

    /**
     * 使用递归方法建树
     *
     * @param treeNodes
     * @return
     */
    public List<OriginalPlaceEntity> buildByRecursive(List<OriginalPlaceEntity> treeNodes) {
        List<OriginalPlaceEntity> trees = new ArrayList<OriginalPlaceEntity>();
        for (OriginalPlaceEntity treeNode : treeNodes) {
            if ("0".equals(treeNode.getParentCode())) {
                trees.add(findChildren(treeNode, treeNodes));
            }
        }
        return trees;
    }


    /**
     * 递归查找子节点
     *
     * @param treeNodes
     * @return
     */
    public OriginalPlaceEntity findChildren(OriginalPlaceEntity treeNode, List<OriginalPlaceEntity> treeNodes) {
        for (OriginalPlaceEntity it : treeNodes) {
            if (treeNode.getAreaCode().equals(it.getParentCode())) {
                if (treeNode.getSecondCombine() == null) {
                    treeNode.setSecondCombine(new ArrayList<OriginalPlaceEntity>());
                }
                treeNode.getSecondCombine().add(findChildren(it, treeNodes));
            }
        }
        return treeNode;
    }

    public List<BaseCountry> buildCountryList(List<BaseCountry> treeNodes) {
        List<BaseCountry> trees = new ArrayList<>();
        for (BaseCountry treeNode : treeNodes) {
            if (treeNode.getParentId() == 0) {
                trees.add(findCountryChildren(treeNode, treeNodes));
            }
        }
        return trees;
    }

    public BaseCountry findCountryChildren(BaseCountry treeNode, List<BaseCountry> treeNodes) {
        for (BaseCountry it : treeNodes) {
            if (treeNode.getCountryId().equals(it.getParentId())) {
                if (treeNode.getChildList() == null) {
                    treeNode.setChildList(new ArrayList<BaseCountry>());
                }
                treeNode.getChildList().add(findCountryChildren(it, treeNodes));
            }
        }
        return treeNode;
    }
}
